package com.powernode.teatraceability.controller.processmgt;


import com.powernode.teatraceability.pojo.login.User;
import com.powernode.teatraceability.pojo.processmgt.StorageMgt;
import com.powernode.teatraceability.service.processmgt.StorageMgtService;
import com.powernode.teatraceability.utils.GlobalConfig;
import com.powernode.teatraceability.utils.JsonData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/storage/mgt")
public class StorageMgtController {
    @Resource
    private StorageMgtService storageMgtService;

    @GetMapping
    public JsonData selectAllStorage(@SessionAttribute(name = "user", required = true) User user){
        storageMgtService.queryAllStorage(user.getQydm());
        return GlobalConfig.LOCAL.get();
    }

    @GetMapping("/{storageName}/{pageNum}/{pageSize}")
    public JsonData selectLike(@PathVariable int pageSize,
                               @PathVariable int pageNum,
                               @PathVariable String storageName,
                               @SessionAttribute(name = "user", required = true) User user){
        GlobalConfig.initPageRequest(pageNum, pageSize);
        storageMgtService.selectLike(storageName, user.getQydm());
        return GlobalConfig.LOCAL.get();
    }

    @GetMapping("/{pageNum}/{pageSize}")
    public JsonData selectAll(@PathVariable int pageNum,
                              @PathVariable int pageSize,
                              @SessionAttribute(name = "user", required = true) User user){
        GlobalConfig.initPageRequest(pageNum,pageSize);
        storageMgtService.selectAll(user.getQydm());
        return GlobalConfig.LOCAL.get();
    }

    @DeleteMapping("/{id}")
    public JsonData delete(@PathVariable int id){
        storageMgtService.delete(id);
        return GlobalConfig.LOCAL.get();
    }

    @PutMapping
    public JsonData update(@RequestBody StorageMgt storageMgt){
        storageMgtService.update(storageMgt);
        return GlobalConfig.LOCAL.get();
    }

    @PostMapping
    public JsonData insert(@RequestBody StorageMgt storageMgt,
                           @SessionAttribute(name = "user", required = true) User user){
        storageMgt.setQydm(user.getQydm());
        storageMgtService.insert(storageMgt);
        return GlobalConfig.LOCAL.get();
    }

}
